//
//  HotRecommendTableViewCell.m
//  TestApplication
//
//  Created by Hanlingyu on 16/8/30.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "HotRecommendTableViewCell.h"
#import "TAMacro.h"
#import "Masonry.h"
#import "HotRecommendCollectionViewCell.h"
#import "Recommend.h"
#import "Posts.h"
#import "UIImageView+WebCache.h"
#import "Img.h"
#import "UIView+Frame.h"
#import "SelfSizeTool.h"
@interface HotRecommendTableViewCell ()
<
    UICollectionViewDelegate,
    UICollectionViewDataSource,
    UICollectionViewDelegateFlowLayout
>


@end


@implementation HotRecommendTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
        
    }
    return self;
}

- (void)createView {
    
    self.contentView.backgroundColor = [UIColor colorWithWhite:0.917 alpha:1.000];
    _backView = [[UIView alloc] init];
    _backView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_backView];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.backgroundColor = [UIColor colorWithWhite:0.896 alpha:1.000];
    _titleLabel.font = [UIFont systemFontOfSize:14];
    _titleLabel.textColor = [UIColor grayColor];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.layer.cornerRadius = 4;
    _titleLabel.layer.masksToBounds = YES;
    [_backView addSubview:_titleLabel];
    
    _contentDetailLabel = [[UILabel alloc] init];
    _contentDetailLabel.font = [UIFont systemFontOfSize:12];
    _contentDetailLabel.textColor = [UIColor lightGrayColor];
    [_backView addSubview:_contentDetailLabel];
    
    
    _attentionButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _attentionButton.backgroundColor = [UIColor cyanColor];
    [_attentionButton addTarget:self action:@selector(attentAction:) forControlEvents:UIControlEventTouchUpInside];
    [_attentionButton setTitle:@"关注" forState:UIControlStateNormal];
    _attentionButton.titleLabel.font = [UIFont systemFontOfSize:14 weight:2];
    _attentionButton.titleLabel.textColor = [UIColor whiteColor];
    _attentionButton.layer.cornerRadius = 4;
    _attentionButton.layer.masksToBounds = 4;
    [_backView addSubview:_attentionButton];
    
    
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.itemSize = CGSizeMake((SCREEN_WIDTH - 44) / 3, 50);
    _layout.minimumLineSpacing = 1.5;
    _layout.minimumInteritemSpacing = 1.5;

    _hotRecommendCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
    _hotRecommendCollectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _hotRecommendCollectionView.delegate = self;
    _hotRecommendCollectionView.dataSource = self;
    _hotRecommendCollectionView.backgroundColor = [UIColor whiteColor];
    [_hotRecommendCollectionView registerClass:[HotRecommendCollectionViewCell class] forCellWithReuseIdentifier:@"hotRecommendCollectionViewCellReuse"];
    [_backView addSubview:_hotRecommendCollectionView];
    
    
    
}

- (void)attentAction:(UIButton *)attentionButton {
    
    NSLog(@"关注");
//    _attentionButton.titleLabel.font = [UIFont systemFontOfSize:14 weight:2];
//    _attentionButton.titleLabel.textColor = [UIColor lightGrayColor];    [_attentionButton removeTarget:self action:@selector(attentAction:) forControlEvents:UIControlEventTouchUpInside];
    
    _attentionButton.backgroundColor = [UIColor clearColor];
    _attentionButton.titleLabel.textColor = [UIColor redColor];
    [_attentionButton setTitle:@"已关注" forState:UIControlStateNormal];
     [_attentionButton addTarget:self action:@selector(cananlAttentAction:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)cananlAttentAction:(UIButton *)attentionButton {
    NSLog(@"取消关注");
    _attentionButton.backgroundColor = [UIColor cyanColor];
    _attentionButton.titleLabel.textColor = [UIColor whiteColor];
    [_attentionButton setTitle:@"关注" forState:UIControlStateNormal];
    [_attentionButton removeTarget:self action:@selector(cananlAttentAction:) forControlEvents:UIControlEventTouchUpInside];
    [_attentionButton addTarget:self action:@selector(attentAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HotRecommendCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"hotRecommendCollectionViewCellReuse" forIndexPath:indexPath];
    NSString *f = _recommend.posts[indexPath.row].img.firstObject.f;
    [cell.hotImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://qn.zheli.io/%@", f]]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *ID = _recommend.posts[indexPath.row].ID;
    self.postChannelDetailPage(ID);
}

// 给界面赋值
- (void)setRecommend:(Recommend *)recommend {
    _recommend = recommend;
    _contentDetailLabel.text = _recommend.desc;
    _titleLabel.text = _recommend.content;
    [self handleTitleLabelWidthAndHeight];
    [self handleHeight];
    [_hotRecommendCollectionView reloadData];
//    [self.cellHeightDelegate heigthOfCell:320];
}
// 处理titleLabel的宽和高, 根据字符串自适应;
- (void)handleTitleLabelWidthAndHeight {
    CGFloat widthOfRecommend_desc = [SelfSizeTool selfSizeWidthWithString:_recommend.content height:20 font:[UIFont systemFontOfSize:14]];
    CGFloat widthOfTitleLabel = widthOfRecommend_desc + 20.0;
    CGFloat heightOfTitleLabel = 25.0;
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(widthOfTitleLabel);
        make.height.mas_equalTo(heightOfTitleLabel);
    }];
}
// 计算每一行三个图片的高
- (void)handleHeight {
    Img *firstImg = _recommend.posts[0].img.firstObject;
    Img *secImg = _recommend.posts[1].img.firstObject;
    Img *thirdImg = _recommend.posts[2].img.firstObject;
    Img *fourthImg = _recommend.posts[3].img.firstObject;
    Img *fifthImg = _recommend.posts[4].img.firstObject;
    Img *sixthImg = _recommend.posts[5].img.firstObject;
    CGFloat temp = firstImg.w / firstImg.h + secImg.w / secImg.h + thirdImg.w / thirdImg.h;
    self.heightOfFirstLine = (SCREEN_WIDTH - 44) / temp;
    CGFloat temp2 = fourthImg.w / fourthImg.h + fifthImg.w / fifthImg.h + sixthImg.w / sixthImg.h;
    self.heightOfSecLine = (SCREEN_WIDTH - 44) / temp2;

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    Img *img = _recommend.posts[indexPath.row].img.firstObject;
    if (indexPath.row < 3) {
        CGFloat height = _heightOfFirstLine;
        CGFloat width = img.w / img.h * _heightOfFirstLine;
        return CGSizeMake(width, height);
    } else {
        CGFloat height = _heightOfSecLine;
        CGFloat width = img.w / img.h * _heightOfSecLine;
        return CGSizeMake(width, height);
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.backView.frame = CGRectMake(10, 10, SCREEN_WIDTH - 20, self.contentView.ltq_height - 10);
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(self.backView).mas_equalTo(15);
    }];
    [self.attentionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backView).mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(25);
    }];
    
    [self.contentDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.backView).mas_equalTo(15);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_equalTo(5);
    }];
    [self.hotRecommendCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentDetailLabel.mas_bottom).mas_equalTo(5);
        make.left.mas_equalTo(self.backView).mas_equalTo(10);
        make.right.mas_equalTo(self.backView).mas_equalTo(-10);
        make.height.mas_equalTo(self.heightOfFirstLine + self.heightOfSecLine + 2);
    }];

}

- (void)layoutIfNeeded {
    [super layoutIfNeeded];

    
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
